package hello;

import exception.ExceptionCharIsNotDefine;
import exception.ExceptionStringIsEmpty;
import exception.StringException;

public class A11StringUtil {

	/**
	 * deleteStr is method using to delete a target String in given String
	 * 
	 * @param strTaget
	 *            : Target string
	 * @param str
	 *            : Specified text
	 * @return
	 */
	public static String deleteStr(String strTaget, String str) {
		if (str == null) {
			return null;
		}
		if (strTaget.isEmpty()) {
			throw new StringException("error");
		}
		if ((str != null) && (strTaget != null)) {
			return str.replaceAll(strTaget, "");
		}
		return null;
	}

	/**
	 * 
	 * @param str
	 * @param startldx
	 * @param length
	 *            : Cut the number of bytes
	 * @return
	 */
	public static String substrByteLength(String str, int startdx, int length) {
		if (str == null || str.equals(""))
			return str;
		StringBuffer result = new StringBuffer(str.length());
		int resultlen = 0;
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (i == startdx) {
				int charlen = 0;
				if (c <= 0x7f) {
					// System.out.println("1--"+c);
					charlen = 1;
				} else if (c <= 0x7ff) {
					charlen = 2;
				} else if (c <= 0xd7ff) {
					charlen = 3;
				} else if (c <= 0xdbff) {
					charlen = 4;
				} else if (c <= 0xdfff) {
					charlen = 0;
				} else if (c <= 0xffff) {
					charlen = 3;
				}
				// System.out.println("charlen : " + charlen);
				resultlen = length / charlen + startdx;
				if (resultlen >= str.length())
					break;
				for (i = startdx; i < resultlen; i++) {
					result.append(str.charAt(i));
				}
				break;
			}
			// System.out.println("i : " + i);
			// result.append(str.charAt(i));
		}
		return result.toString();
	}

	/**
	 * This function use to count how many character appear in string.
	 * @param str : String 
	 * @param strTargetChar : character
	 * @return integer
	 */
	public static int countChar(String str, char strTargetChar) {
		int count = 0;
		if (str == null)
			return 0;
		if (str.isEmpty())
			throw new ExceptionStringIsEmpty("empty");
		
		//Check character excepted.
		if (((64 < strTargetChar) && (strTargetChar < 91)) || ((96 < strTargetChar) && (strTargetChar < 123))) {
			int length = str.length();
			for (int i = 0; i < length; i++) {
				if (str.charAt(i) == strTargetChar) {
					count++;
				}
			}

		}

		else
			throw new ExceptionCharIsNotDefine("char is out range");
		return count;
	}

	/**
	 * 
	 * @param str
	 * @param strtargetWord
	 * @return
	 */
	public static int countWord(String str, String strtargetWord) {
		if (str == null)
			return 0;
		if (str.isEmpty())
			throw new ExceptionStringIsEmpty("str is empty");
		if (strtargetWord == null)
			return 0;
		if (strtargetWord.isEmpty())
			throw new ExceptionStringIsEmpty("strtargetWord is empty");
		int strtargetLength = strtargetWord.length();
		int strLength = str.length();
		if (strtargetLength > strLength)
			return 0;
		for (int i = 0; i < strtargetLength; i++) {
			char c = strtargetWord.charAt(i);
			if ((c < 65) || (c > 90 && c < 97) || c > 122) {
				throw new ExceptionCharIsNotDefine("Charater is not define");
			}
		}
		int count = 0;
		int idx = 0;
		while ((idx = str.indexOf(strtargetWord, idx)) != -1) {
			count++;
			idx += strtargetWord.length();
		}
		return count;
	}

}
